package
{
	import crl.menu.MainMenu;
	import crl.menu.SubMenu;
	import crl.menu.data.Config;
	import crl.menu.events.MenuEvent;
	import crl.menu.utils.XMLAsset;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	[SWF(width="1100",height="220", backgroundColor="0x333333")]
	public class menu extends Sprite
	{
		
		//[Embed("assets/cfg.xml",mimeType="application/octet-stream")]
		//private static const Asset:Class;
		
		private var app:*;
		
		private var main:MainMenu;
		private var sub:SubMenu;
		public function menu()
		{
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			load();
			//embed();
		}
		
		
		private function load():void{
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE,completeHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			
			
			var cfg:String=loaderInfo.parameters["cfg"];
			if(!cfg){
				cfg="assets/cfg.xml";
			}
			
			loader.load(new URLRequest(cfg));
		}
		
		
		private var errorTF:TextField;
		private function errorHandler(event:Event):void{
				if(errorTF==null){
					errorTF=new TextField();
					errorTF.autoSize=TextFieldAutoSize.LEFT;
					errorTF.background=true;
					errorTF.backgroundColor=0x990000;
					errorTF.textColor=0xFFFFFF;
				}
				
				errorTF.text=event.toString();
				
				errorTF.x=(stage.stageWidth-errorTF.textWidth)>>1;
				errorTF.y=(stage.stageHeight-errorTF.textHeight)>>1;
		}
		
		
		private function embed():void{
			//var config:XMLAsset = new XMLAsset( Asset );
		
			//initConfiguration(config.xml);
		}
		
		private function completeHandler(event:Event):void{
			
			var xml:XML=new XML((event.target as URLLoader).data);
			
			
			initConfiguration(xml);
		}
		
		private function initConfiguration(xml:XML):void{
			Config.instance.init(xml);
			
			var request:URLRequest=new URLRequest(Config.instance.ui);
			
			var loader:Loader=new Loader();
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,uiCompleteHandler);
			
			loader.load(request,new LoaderContext(false,ApplicationDomain.currentDomain));
		}
		
		private function uiCompleteHandler(event:Event):void{
			initialize();
		}
		
		private function initialize():void{
			app=new Application();
			app.x=74>>1;
			main=new MainMenu(app);
			sub=new SubMenu(app);
			main.addEventListener(Event.OPEN,openHandle);
			main.addEventListener(Event.CLOSE,sub.close);
			
			addChild(app);
		}
		
		private function openHandle(event:MenuEvent):void{
			sub.open(event.index);
		}
		
		
	}
}